<template>
  <div class="panel_left">
    <div class="plugin_item"
         v-for="(item, index) in pluginList"
         :key="item.id"
         :class="{active: currentIndex == index}"
         @click="changeIndex(index)">
      <div class="plugin_item_logo">
        <div class="plugin_item_logo_img">
          <img :src="item.logo ? (item.logo.match(/^https?:\/\//) ? item.logo : $localPluginImage(item)) : $localImage('logo')"
               alt="">
          <!-- <img :src="item.logo ? (item.logo.match(/^https?:\/\//) ? item.logo : $localPluginImage(item)) : $localImage('logo')"
               alt=""> -->
        </div>
      </div>
      <div class="plugin_item_info">
        <div class="plugin_item_info_title">
          <div class="plugin_item_info_title_text"
               v-text="item.pluginName"></div>
        </div>
        <div class="plugin_item_info_subtitle"
             v-text="item.description"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PanelList',
  props: {
    list: {
      type: Array,
      default () {
        return []
      }
    },
    activeIndex: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      pluginList: [],
      currentIndex: -1
    }
  },
  watch: {
    list: {
      immediate: true,
      handler (val) {
        this.pluginList = JSON.parse(JSON.stringify(this.list))
      }
    },
    activeIndex: {
      immediate: true,
      handler (val) {
        this.currentIndex = val
      }
    }
  },
  mounted () {
    // eventBus.$on('plugin-remove-response', this.pluginRemoveResponse)
    bbtools.on('plugin-remove-response', this.pluginRemoveResponse)
    bbtools.on('plugin-uninstall-response', this.pluginRemoveResponse)
    // eventBus.$on('local-plugin-reload', this.localPluginReloaded)
  },
  methods: {
    changeIndex (index) {
      this.$emit('change', index)
    },
    // localPluginReloaded (data) {
    //   console.log('>>>>>', data)
    //   for (let i = 0; i < this.pluginList.length; i++) {
    //     if (this.pluginList[i].id == data.id) {
    //       this.pluginList.splice(i, 1, data)
    //       i = this.pluginList.length
    //     }
    //   }
    //   console.log('=====', this.pluginList)
    // },
    pluginRemoveResponse (event, response) {
      let pindex = -1
      for (let i = 0; i < this.pluginList.length; i++) {
        if (this.pluginList[i].id == response.data.id) {
          pindex = i
          i = this.pluginList.length
        }
      }
      if (pindex > -1) {
        if (pindex == this.pluginList.length - 1) {
          this.currentIndex = this.pluginList.length - 2
          this.changeIndex(this.currentIndex)
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
@import url("../../themes/index.less");
.panel_left {
  position: absolute;
  left: 0;
  width: 200px;
  height: 100%;
  background-color: lighten(@darkDividerColor, 85);
  overflow-y: auto;
  box-shadow: -20px -5px 22px -22px rgba(217, 217, 217, 0.8) inset;
  .plugin_item {
    width: 100%;
    height: 56px;
    cursor: pointer;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    &:hover {
      background-color: @activeBgColor;
    }
    &.active {
      background-color: @activeBgColor;
    }
    &_logo {
      width: 56px;
      height: 56px;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      &_img {
        width: 36px;
        height: 36px;
        background-color: white;
        border-radius: 5px;
        overflow: hidden;
        padding: 4px;
        box-sizing: border-box;
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }
    &_info {
      width: calc(~"100% - 56px - 15px");
      height: 100%;
      display: flex;
      align-items: center;
      flex-direction: column;
      justify-content: center;
      &_title {
        width: 100%;
        height: 16px;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: flex-start;
        font-size: 12px;
        color: @darkTextColor1;
      }
      &_subtitle {
        width: 100%;
        height: 14px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        font-size: 10px;
        color: @darkTextColor3;
      }
    }
  }
}
</style>